package com.yxf.variousshapes3d.shapes;

import android.graphics.Color;

import com.yxf.variousshapes3d.Point;

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import static android.opengl.GLES20.GL_TRIANGLES;
import static android.opengl.GLES20.GL_UNSIGNED_BYTE;
import static android.opengl.GLES20.glDrawElements;

public class Tetrahedron extends BaseShape {
    private float[] vertices;
    private FloatBuffer vertexData;
    private float radius;

    private ByteBuffer positionBuffer;

    private int coordinateCount;

    public Tetrahedron(Point center, float radius) {
        super(center);
        vertices = new float[(COLOR_COMPONENT_COUNT + POSITION_COMPONENT_COUNT) * 4];
        this.radius = radius;
        //绘制时所需顶点个数
        coordinateCount = 3 * 4;
    }

    @Override
    public void initWithoutLock() {
        //边长
        float l = (float) (radius * 4 / Math.sqrt(6));
        //底部三角形高
        float bh = (float) (Math.sqrt(3) / 2 * l);
        //底部三角形中点距离底部三角形三个交点的距离
        float cl = bh * 2 / 3;
        //正四面体的高
        float th = (float) Math.sqrt(l * l - cl * cl);
        //底面距离四面体中心的距离,由(ch + radius) = th 和 radius * radius = cl * cl + ch * ch 可求得ch的值
        float ch = (float) (l / (2 * Math.sqrt(6)));
        int i = 0;
        int color;
        //底部三角形上顶点,0
        vertices[i++] = center.x;
        vertices[i++] = center.y + bh * 2 / 3;
        vertices[i++] = center.z - ch;
        color = Color.BLUE;
        vertices[i++] = Color.red(color) / 255f;
        vertices[i++] = Color.green(color) / 255f;
        vertices[i++] = Color.blue(color) / 255f;
        vertices[i++] = Color.alpha(color) / 255f;
        //底部三角形左顶点,1
        vertices[i++] = center.x - l / 2;
        vertices[i++] = center.y - bh / 3;
        vertices[i++] = center.z - ch;
        color = Color.GREEN;
        vertices[i++] = Color.red(color) / 255f;
        vertices[i++] = Color.green(color) / 255f;
        vertices[i++] = Color.blue(color) / 255f;
        vertices[i++] = Color.alpha(color) / 255f;
        //底部三角形右顶点,2
        vertices[i++] = center.x + l / 2;
        vertices[i++] = center.y - bh / 3;
        vertices[i++] = center.z - ch;
        color = Color.YELLOW;
        vertices[i++] = Color.red(color) / 255f;
        vertices[i++] = Color.green(color) / 255f;
        vertices[i++] = Color.blue(color) / 255f;
        vertices[i++] = Color.alpha(color) / 255f;
        //上定点,3
        vertices[i++] = center.x;
        vertices[i++] = center.y;
        vertices[i++] = center.z + radius;
        color = Color.RED;
        vertices[i++] = Color.red(color) / 255f;
        vertices[i++] = Color.green(color) / 255f;
        vertices[i++] = Color.blue(color) / 255f;
        vertices[i++] = Color.alpha(color) / 255f;
        vertexData = encodeVertices(vertices);

        positionBuffer = ByteBuffer.allocateDirect(coordinateCount)
                .put(new byte[]{
                        0, 2, 1,
                        0, 1, 3,
                        2, 0, 3,
                        1, 2, 3,
                });
        positionBuffer.position(0);
    }

    @Override
    protected FloatBuffer getVertexData() {
        return vertexData;
    }

    @Override
    protected void drawArrays() {
        glDrawElements(GL_TRIANGLES, coordinateCount, GL_UNSIGNED_BYTE, positionBuffer);
    }

    @Override
    protected void preDraw() {

    }

    @Override
    protected void afterDraw() {

    }
}
